//
//  posterView.m
//  HuiWenMovie
//
//  Created by niceDay on 15/7/24.
//  Copyright (c) 2015年 computer. All rights reserved.
//

#import "posterView.h"
#import "UIViewExt.h"

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
#define KheghtNav 64
#define KheghtTab 49
#define kHeaderViewH 136
#define kIndexCollectionViewH 100
#define kBottomTitleLableH 35
#define kBottomTitleLableY (kHeight-KheghtTab-kBottomTitleLableH)
#define kHeaderOffH   36
#define kPosterColletionViewY  (kHeaderViewH-kHeaderOffH)
#define kPosterColletionViewH  (kHeight-kPosterColletionViewY-KheghtTab-kBottomTitleLableH)

@implementation posterView

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self _createPosterCollectionView];
        [self _createBottomTitleLable];
        [self _createControl];
        [self _createHeaderView];
        [self _createBottomTitleLable];
        [self _createSwipDownGesture];
        
        [_posterCollectionView addObserver:self forKeyPath:@"currentIndex" options:NSKeyValueObservingOptionNew context:nil];
        [_indexCollectionView addObserver:self forKeyPath:@"currentIndex" options:NSKeyValueObservingOptionNew context:nil];
        
    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    
    NSNumber *number = [change objectForKey:@"new"];
    NSInteger index = [number integerValue];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    
    if ([object isKindOfClass:[_posterCollectionView class]]&&_indexCollectionView.currentIndex != index) {
        [_indexCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        _indexCollectionView.currentIndex = index;
    }
    else if ([object isKindOfClass:[_indexCollectionView class]]&&_posterCollectionView.currentIndex != index){
        [_posterCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        _posterCollectionView.currentIndex = index;
    }
    
    messageModal *modal = _movieModalArray[index];
    _bottomTitleLable.text = modal.title;
    
    
}

#pragma mark - _createSubviews
- (void)_createHeaderView{
    _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -kHeaderOffH, kWidth, kHeaderViewH)];
    _headerView.backgroundColor = [UIColor blackColor];
    UIImage *image = [UIImage imageNamed:@"indexBG_home"];
    [image stretchableImageWithLeftCapWidth:0 topCapHeight:1];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, kWidth, 36)];
    imageView.image = image;
    [_headerView addSubview:imageView];
    [self addSubview:_headerView];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection =  UICollectionViewScrollDirectionHorizontal;

//    layout.itemSize = CGSizeMake(kWidth*0.2,kIndexCollectionViewH);
//    layout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;

   _indexCollectionView = [[indexCollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kIndexCollectionViewH) collectionViewLayout:layout];
    _indexCollectionView.itemWidth = kWidth/5;
    _indexCollectionView.backgroundColor = [UIColor blackColor];
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 0, 60,  kIndexCollectionViewH)];
    leftImageView.image = [UIImage imageNamed:@"light"];
    UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth-120, 0, 60,  kIndexCollectionViewH)];
    rightImageView.image = [UIImage imageNamed:@"light"];
    
    [_headerView addSubview:_indexCollectionView];
    
    
    [_headerView addSubview:leftImageView];
    [_headerView addSubview:rightImageView];

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake((kWidth-20)/2, kHeaderViewH-30, 25, 25)];
 //   button.backgroundColor = [UIColor redColor];
    [button setImage:[UIImage imageNamed:@"down_home"] forState:UIControlStateNormal];
     [button setImage:[UIImage imageNamed:@"up_home"] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headerView addSubview:button];
    
}

- (void)_createPosterCollectionView{
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection =  UICollectionViewScrollDirectionHorizontal;
//    layout.itemSize = CGSizeMake(kWidth*0.75,kPosterColletionViewH);
//    layout.sectionInset = UIEdgeInsetsMake(0, kWidth/8, 0, kWidth/8);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    _posterCollectionView = [[posterCollectionView alloc] initWithFrame:CGRectMake(0, kPosterColletionViewY, kWidth, kPosterColletionViewH) collectionViewLayout:layout];
    _posterCollectionView.backgroundColor = [UIColor blackColor];
    _posterCollectionView.itemWidth = kWidth*3/4;
  //  _posterCollectionView.pagingEnabled = YES;
    [self addSubview:_posterCollectionView];
    
    
    
}

- (void)_createBottomTitleLable{
    _bottomTitleLable = [[UILabel alloc] initWithFrame:CGRectMake(0,kBottomTitleLableY, kWidth, kBottomTitleLableH)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:_bottomTitleLable.bounds];
    imageView.image = [UIImage imageNamed:@"nav_bg_all-64"];
    [_bottomTitleLable addSubview:imageView];
   // _bottomTitleLable.backgroundColor = [UIColor blackColor];
    _bottomTitleLable.textColor = [UIColor whiteColor];
    _bottomTitleLable.font = [UIFont systemFontOfSize:25.0];
    _bottomTitleLable.textAlignment =     NSTextAlignmentCenter    ;
    [self addSubview:_bottomTitleLable];
    
    
}


- (void)_createControl{
    _control = [[UIControl alloc] initWithFrame:self.bounds];
    _control.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.8];
    [_control addTarget:self action:@selector(controlAction) forControlEvents:UIControlEventTouchUpInside];
    _control.hidden = YES;
    [self addSubview:_control];
    
    
}

- (void)_createSwipDownGesture{
    UISwipeGestureRecognizer *swipDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipDownAction)];
    swipDown.direction =  UISwipeGestureRecognizerDirectionDown;
    [self addGestureRecognizer:swipDown];
    
}

#pragma mark - tool

- (void)controlAction{
    _control.hidden = YES;
    _headerView.top = -kHeaderOffH;
    
}


- (void)swipDownAction{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.3];
    [self showHeaderView];
    [UIView commitAnimations];
}

- (void)pushAction:(UIButton *)btn{
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.3];
    
    if (btn.selected == NO) {
        [self showHeaderView];
    }
    else{
        [self hideHeaderView];
    }
    btn.selected = !btn.selected;
    [UIView commitAnimations];
}
#pragma mark - show-hide headerVIew
- (void)showHeaderView{
    _headerView.top = KheghtTab;
    _control.hidden = NO;
}

- (void)hideHeaderView{
    _headerView.top = -kHeaderOffH;
    _control.hidden = YES;

}

#pragma mark - setModalArray
- (void)setMovieModalArray:(NSArray *)movieModalArray{
    _movieModalArray = movieModalArray;
  
    messageModal *modal = _movieModalArray[0];
    _bottomTitleLable.text = modal.title;
    
    _posterCollectionView.movieModalArray = _movieModalArray;
    
    _indexCollectionView.movieModalArray = _movieModalArray;
    
}
@end
